package com.hhd.pdf.components.table;

import com.hhd.pdf.components.AbstractPdfComponent;
import com.hhd.pdf.context.GeneratorContext;
import com.hhd.pdf.utils.PdfUtils;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfDiv;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.hhd.pdf.CommonUtils;
import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.List;

//@Component
public class TableComponent extends AbstractPdfComponent {

    @Override
    public void parseComponent(Element element, GeneratorContext context) throws Exception {
        TableXmlParser tableXmlParser = new TableXmlParser(element);
        TableVo tableVo = tableXmlParser.getTable();
        List<TrVo> trs = tableVo.getTrs();
        PdfPTable pdfPTable = createTable(tableVo, element, context, trs);
        int row = 0, col = 0;
        for (TrVo tr : trs) {
            List<TdVo> tds = tr.getTds();
            col = 0;
            for (TdVo td : tds) {
                String text = td.getTd().getText();
                PdfPCell cell = PdfUtils.createCell();
                Paragraph paragraph = PdfUtils.createParagraph(text, context.getGeneratorParameters().getFont(), Paragraph.ALIGN_CENTER);

                if (tr.getType() == TrVo.TrType.head || tr.getType() == TrVo.TrType.foot) {
                    cell.setBackgroundColor(PdfUtils.createColor(250, 250, 250));
                }

                cell.setPaddingLeft(5);
                cell.setPaddingRight(5);
                cell.setPaddingTop(0);
                cell.setBorderColor(context.getGeneratorParameters().getBorderColor());
                cell.setBorder(1);
                cell.setVerticalAlignment(com.itextpdf.text.Element.ALIGN_MIDDLE);

                if (row == 0) {
                    cell.setBorderWidthTop(1);
                }
                if (col == 0) {
                    cell.setBorderWidthLeft(1);
                }
                cell.setBorderWidthRight(1);
                cell.setBorderWidthBottom(1);

                cell.setColspan(td.getColSpan());
                cell.setHorizontalAlignment(td.getAlign());
                paragraph.setAlignment(td.getAlign());

                cell.setMinimumHeight(35);

                cell.addElement(paragraph);
                pdfPTable.addCell(cell);
                col++;
            }
            row++;
        }
        PdfDiv pdfDiv = PdfUtils.wrapDiv(pdfPTable, 15, 30, 10, 30);
        context.getDocument().add(pdfDiv);
    }

    private PdfPTable createTable(TableVo table,
                                  Element element,
                                  GeneratorContext context,
                                  List<TrVo> trs) {
        TrVo trVo = trs.get(0);
        List<TdVo> tds = trVo.getTds();
        int colNum = 0;
        colNum = getColNum(tds, colNum);
        return PdfUtils.createTable(colNum, context.getGeneratorParameters().getInitPageWidth());
    }

    private static int getColNum(List<TdVo> tds, int colNum) {
        for (TdVo td : tds) {
            Attribute colSpan = td.getTd().attribute("colSpan");
            if (!CommonUtils.isEmpty(colSpan)) {
                String value = colSpan.getValue();
                colNum += Integer.parseInt(value);
            } else {
                colNum++;
            }
        }
        return colNum;
    }

    @Override
    public String getName() {
        return "table";
    }
}
